//
//  DNNotification.m
//  DanoneEA
//
//  Created by Senela on 16/11/3.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//

#import "DNNotificationDao.h"

@implementation DNNotificationDao
+ (DNNotificationDao *)shareInstance
{
    static DNNotificationDao *sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

-(BOOL)saveNotificationModel:(DNNotificationModel *)object
{
    DNDataBase *dataBase = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    
    [dataBase.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            DDLogError(@"DNNotificationDao -- saveNotificationModel open db error: %@", db.lastErrorMessage);
            return;
        }else
            [db setKey:DB_SECRETKEY];
        
        ret = [db executeUpdate:[self getInsertSQL],
               object.serverId,
               object.modifyReason,
               object.deleteReason,
               object.createTime,
               object.modifyTime,
               [NSString stringWithFormat:@"%d", object.type],
               [NSString stringWithFormat:@"%d",object.mode],
               object.time,
               object.motherId
               ];
        
        if (!ret) {
            [db rollback];;
            DDLogError(@"DNNotificationDao -- saveNotificationModel error:%@",
                       db.lastErrorMessage);
            [db close];
            return;
        }
        
        [db close];
    }];
    
    return ret;
}

-(BOOL)deleteNotificationModel:(DNNotificationModel *)object
{
    DNDataBase *dnDB = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            DDLogError(@"DNNotificationDao -- deleteNotificationModel open db error: %@", db.lastErrorMessage);
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString * deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = %@;",Notification_TABLE, DairyBase_ServerId, object.serverId];
        ret = [db executeUpdate:deleteSQL];
        
        if (!ret) {
            [db rollback];
            DDLogError(@"DNNotificationDao -- deleteNotificationModel error:%@",
                       db.lastErrorMessage);
            [db close];
            return;
        }
        
        [db close];
        
    }];
    return ret;
}

-(BOOL)saveNotificationArr:(NSArray *)notificationArr
{
    __block BOOL ret = YES;
    DNDataBase *dataBase = [DNDataBase shareInstance];
    // 如果要支持事务
    [dataBase.dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        [db setKey:DB_SECRETKEY];
        
        for (DNNotificationModel *object in notificationArr) {
            
            ret =[db executeUpdate:[self getInsertSQL],
                  object.serverId,
                  object.modifyReason,
                  object.deleteReason,
                  object.createTime,
                  object.modifyTime,
                  [NSString stringWithFormat:@"%d", object.type],
                  [NSString stringWithFormat:@"%d",object.mode],
                  object.time,
                  object.motherId
                  ];
            
            
            if (!ret) {
                *rollback = YES;
                DDLogError(@"DNNotificationDao -- saveNotificationArr error:%@",
                           db.lastErrorMessage);
                return;
            }
            
        }
    }];
    
    return ret;
}


-(NSMutableArray *)getNotificationArrByType:(NotifyType)type
{
    DNDataBase *dnDB = [DNDataBase shareInstance];
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString *searchSql =[NSString stringWithFormat:@"SELECT * FROM %@ where %@ = '%@';", Notification_TABLE, Notification_Type, [NSString stringWithFormat:@"%d", type]];
        
        FMResultSet *rs = [db executeQuery:searchSql];
        while ([rs next]) {
            DNNotificationModel *object = [[DNNotificationModel alloc] init];
            
            [object setServerId:[rs stringForColumn:DairyBase_ServerId]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyReason]];
            [object setDeleteReason:[rs stringForColumn:DairyBase_DeleteReason]];
            [object setCreateTime:[rs stringForColumn:DairyBase_CreateTime]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyTime]];
            [object setMode:[[rs stringForColumn:Notification_Mode] intValue]];
            [object setType:[[rs stringForColumn:Notification_Type] intValue]];
            [object setMotherId:[rs stringForColumn:Notification_diaryreg_Id]];
            [object setTime:[rs stringForColumn:Notification_time]];
            
            //添加对象到集合中
            [items addObject:object];
        }
        [db close];
    }];
    
    return items;
    
}

-(NSMutableArray *)getAllNotificationArr
{
    DNDataBase *dnDB = [DNDataBase shareInstance];
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString *searchSql =[NSString stringWithFormat:@"SELECT * FROM %@ ", Notification_TABLE];
        
        FMResultSet *rs = [db executeQuery:searchSql];
        while ([rs next]) {
            DNNotificationModel *object = [[DNNotificationModel alloc] init];
            
            [object setServerId:[rs stringForColumn:DairyBase_ServerId]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyReason]];
            [object setDeleteReason:[rs stringForColumn:DairyBase_DeleteReason]];
            [object setCreateTime:[rs stringForColumn:DairyBase_CreateTime]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyTime]];
            [object setMode:[[rs stringForColumn:Notification_Mode] intValue]];
            [object setType:[[rs stringForColumn:Notification_Type] intValue]];
            [object setMotherId:[rs stringForColumn:Notification_diaryreg_Id]];
            [object setTime:[rs stringForColumn:Notification_time]];
            
            //添加对象到集合中
            [items addObject:object];
        }
        [db close];
    }];
    
    return items;
    
}

#pragma mark - Private methods
-(NSString *) getInsertSQL
{
    return [NSString stringWithFormat:@"REPLACE INTO %@(%@,%@,%@,%@,%@,%@,%@,%@,%@) VALUES(?,?,?,?,?,?,?,?,?);",
            Notification_TABLE,
            DairyBase_ServerId,
            DairyBase_ModifyReason,
            DairyBase_DeleteReason,
            DairyBase_CreateTime,
            DairyBase_ModifyTime,
            Notification_Type,
            Notification_Mode,
            Notification_time,
            Notification_diaryreg_Id
            ];
    
}


@end
